// 时间格式转换 yyyy/mm/dd
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 时间格式转换 yyyy－mm－dd
function formatTime2(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 时间格式转换 yyyy－mm－dd
function formatTime3(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [month, day].map(formatNumber).join('-')
}
function getweekday(date){

  var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
  
  var week = weekArray[new Date(date).getDay()];//注意此处必须是先new一个Date
  // if(week == )
  var tod = weekArray[new Date().getDay()]
  if(current(4) == date){
    week = '今天'
  }else if(getNextDate(current(4),1) == date){
    week = '明天'
  }
  // console.log(week - tod)
  // console.log(new Date(date).getDay())
  
  return week;
  
  }
  function getweekday1(date){

    var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    
    var week = weekArray[new Date(date).getDay()];//注意此处必须是先new一个Date
    var tod = weekArray[new Date().getDay()]
    return week;
    
    }
function formatDate(date, split) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join(split || '')
}

// 两位数自动补零
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}



// 计算变化多少天后的日期
function DateAddDay(d, days) {
 
  var d = new Date(d);
  console.log('aa',d)
  return new Date(d.setDate(d.getDate() + days));
}
// 获得本周周日的日期
function FirstDayInThisWeek(d) {
  
  var d = new Date(d);
  // console.log(formatTime(DateAddDay(d, 0 - d.getDay())));
  return DateAddDay(d, 0 - d.getDay());
}
// 传入日期的前一天-1和后一天1
function getNextDate(date, day) {
  var dd = new Date(date);
  dd.setDate(dd.getDate() + day);
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  // console.log(dd)
  return y + "-" + m + "-" + d;
}
// 判断类型
function Type(obj) {
  var typeStr = Object.prototype.toString.call(obj).split(" ")[1];
  return typeStr.substr(0, typeStr.length - 1).toLowerCase();
}
function current(type){
  var date = new Date();
			
			var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
			var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
			var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
			
			var hours = date.getHours();          //小时 ,返回 Date 对象的小时 (0 ~ 23)
			var minutes = date.getMinutes();      //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
			var seconds = date.getSeconds();      //秒 ,返回 Date 对象的秒数 (0 ~ 59)   
      
      if(type == 1){
        //获取当前系统时间  
        var currentDate = year + "-" + month
        return currentDate
      }else if(type == 2){
        //获取当前系统时间  
        var currentDate = year + "年" + month + "月"
        return currentDate
      }else if(type == 3){
        //获取当前系统时间   (月-日)
        var currentDate = month + "-" + day
        return currentDate
      }else if(type == 4){
        var month = month.toString().padStart(2,'0');
        var day = day.toString().padStart(2,'0');
        //获取当前系统时间   (年-月-日)
        var currentDate = year + "-" + month + "-" + day
        return currentDate
      }else if(type == 5){
        var month = month.toString().padStart(2,'0');
        var day = day.toString().padStart(2,'0');
        var hours = hours.toString().padStart(2,'0');
        var minutes = minutes.toString().padStart(2,'0');
        var seconds = seconds.toString().padStart(2,'0');
        //获取当前系统时间  
        var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        return currentDate

      }else{
        //获取当前系统时间  
        var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        return currentDate
      }
			
}
// 时间加几天
function dateAddDays(dateStr,dayCount) {
    var tempDate=new Date(dateStr.replace(/-/g,"/"));//把日期字符串转换成日期格式
     var resultDate=new Date((tempDate/1000+(86400*dayCount))*1000);//增加n天后的日期
     var minutes = (resultDate.getMonth()+1)
     var seconds = (resultDate.getDate())
     minutes = minutes.toString().padStart(2,'0');
     seconds = seconds.toString().padStart(2,'0');
    var resultDateStr=resultDate.getFullYear()+"-"+minutes+"-"+seconds;//将日期转化为字符串格式
     return resultDateStr;
 }
 //两个时间相差天数 兼容firefox chrome
 function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式 
  var dateSpan,
      tempDate,
      iDays;
  sDate1 = Date.parse(sDate1);
  sDate2 = Date.parse(sDate2);
  dateSpan = sDate2 - sDate1;
  dateSpan = Math.abs(dateSpan);
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
  return iDays
}
 function sub(val) {
  return val.slice(0,3)
}
function airport(val) {
  return val.slice(0,2)
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatTime2,
  formatTime3,
  getweekday,
  getNextDate,
  DateAddDay: DateAddDay,
  FirstDayInThisWeek: FirstDayInThisWeek,
  type: Type,
  addZero: formatNumber,
  dateAddDays,
  current: current,
  getweekday1:getweekday1,
  datedifference:datedifference,
  sub:sub,
  airport:airport
}